package com.cdg.runyunyijia;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class LLButton extends androidx.appcompat.widget.AppCompatButton {



    public LLButton(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public LLButton(@NonNull Context context,@NonNull AttributeSet attrs) {
        super(context, attrs);
    }

    public LLButton(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        Drawable[] drawables = getCompoundDrawables();

        if(drawables !=null) {

            Drawable drawableLeft = drawables[0];

            if(drawableLeft !=null) {

                float textWidth = getPaint().measureText(getText().toString());

                int drawablePadding = getCompoundDrawablePadding();

                int drawableWidth =drawableLeft.getIntrinsicWidth();

                float bodyWidth = textWidth + drawableWidth + drawablePadding;

                canvas.translate((getWidth() - bodyWidth) /2,0);
            }
        }
        super.onDraw(canvas);
    }

}
